from time import sleep

from biz.ranzhi.login_page import LoginPage


class MainPage(LoginPage):
    # 定位符

    LOGOUT_AVATAR_SELECTOR = "s, #start > div"
    LOGOUT_MENU_ZH_TEXT = "p, 退出"
    LOGOUT_MENU_EN_TEXT = "p, Logout"
    LOGOUT_MENU_ACCOUNT_SELECTOR = "s, #startMenu > li.with-avatar > a > strong"
    LOGOUT_MENU_LANG_SELECTOR = "s, #startMenu > li:nth-child(3) > a"

    MAIN_BAR_ACCOUNT_SELECTOR = "s, #mainNavbar > div > ul:nth-child(1) > li > a"
    MAIN_BAR_ACCOUNT_DIALOG_REAL_NAME_SELECTOR = "s, #ajaxModal > div.modal-dialog > div > div.modal-body > div > table > tbody > tr:nth-child(1) > td.w-p45"
    MAIN_BAR_ACCOUNT_DIALOG_CLOSE_SELECTOR = "s, #ajaxModal > div.modal-dialog > div > div.modal-header > button"
    MAIN_BAR_MENU_SELECTOR = "s, #mainNavbar > div > ul:nth-child(2) > li:nth-child(%d)"

    LEFT_BAR_APP_CRM_SELECTOR = "s, #s-menu-1 > button > img"
    LEFT_BAR_APP_OA_SELECTOR = "s, #s-menu-2 > button > img"
    LEFT_BAR_APP_PROJECT_SELECTOR = "s, #s-menu-3 > button > img"
    LEFT_BAR_APP_DOC_SELECTOR = "s, #s-menu-4 > button > img"
    LEFT_BAR_APP_CASH_SELECTOR = "s, #s-menu-5 > button > img"
    LEFT_BAR_APP_TEAM_SELECTOR = "s, #s-menu-6 > button > img"
    LEFT_BAR_APP_ADMIN_SELECTOR = "s, #s-menu-superadmin > button > i"

    FRAME_APP_CRM_SELECTOR = "s, #iframe-1"
    FRAME_APP_OA_SELECTOR = "s, #iframe-2"
    FRAME_APP_CASH_SELECTOR = "s, #iframe-5"
    FRAME_APP_PROJECT_SELECTOR = "s, #iframe-3"
    FRAME_APP_DOC_SELECTOR = "s, #iframe-4"
    FRAME_APP_TEAM_SELECTOR = "s, #iframe-6"
    FRAME_APP_ADMIN_SELECTOR = "s, #iframe-superadmin"

    APP_TETLE_SELCTOR = "s, #mainNavbar > div > a"

    NAV_MENU_SELECTOR = "s, #mainNavbar > div > ul:nth-child(2) > li:nth-child(%d)"
    START_BUTTON_SELECTOR = "s, #start > div"
    START_EXIT_MENU_SELECTOR = "s, #startMenu > li:nth-child(%d) > a"

    _login_name = None

    def do_logout_by_lang(self, lang):
        """
        退出
        :return:
        """
        self.base_driver.click(self.LOGOUT_AVATAR_SELECTOR)
        sleep(1)
        if lang == "en":
            self.base_driver.click(self.LOGOUT_MENU_EN_TEXT)
        else:
            self.base_driver.click(self.LOGOUT_MENU_ZH_TEXT)

        sleep(2)

    def select_app(self, app):
        """
        选择 应用
        :param app: oa，crm，cash，team，proj，admin，doc
        :return:
        """
        self.log("[MainPage] 开始 select_app(%s)" % app)
        if app == "oa":
            self.base_driver.click(self.LEFT_BAR_APP_OA_SELECTOR)
        elif app == "crm":
            self.base_driver.click(self.LEFT_BAR_APP_CRM_SELECTOR)
        elif app == "cash":
            self.base_driver.click(self.LEFT_BAR_APP_CASH_SELECTOR)
        elif app == "team":
            self.base_driver.click(self.LEFT_BAR_APP_TEAM_SELECTOR)
        elif app == "proj":
            self.base_driver.click(self.LEFT_BAR_APP_PROJECT_SELECTOR)
        elif app == "admin":
            self.base_driver.click(self.LEFT_BAR_APP_ADMIN_SELECTOR)
        elif app == "doc":
            self.base_driver.click(self.LEFT_BAR_APP_DOC_SELECTOR)
        self.log("[MainPage] 结束 select_app(%s)" % app)
        sleep(2)

    def get_real_name_main_bar(self):
        """
        获取系统在顶层主菜单的真实姓名
        :return:
        """
        return self.base_driver.get_text(self.MAIN_BAR_ACCOUNT_SELECTOR)

    @property
    def current_login_name(self):
        return self._login_name

    def get_real_name_logout_menu(self):
        """
        获取系统在退出登录菜单的真实姓名
        :return:
        """
        self.base_driver.click(self.LOGOUT_AVATAR_SELECTOR)
        sleep(1)
        real_name = self.base_driver.get_text(self.LOGOUT_MENU_ACCOUNT_SELECTOR)

        self.base_driver.click(self.LOGOUT_AVATAR_SELECTOR)
        sleep(1)
        return real_name

    def get_real_name_by_click_main_bar(self):
        """
        点击左上角的真实姓名标签，获取真实姓名
        :return:
        """
        self.base_driver.click(self.MAIN_BAR_ACCOUNT_SELECTOR)
        sleep(2)
        real_name = self.base_driver.get_text(self.MAIN_BAR_ACCOUNT_DIALOG_REAL_NAME_SELECTOR)
        sleep(1)
        self.base_driver.click(self.MAIN_BAR_ACCOUNT_DIALOG_CLOSE_SELECTOR)
        sleep(1)
        return real_name

    def select_menu(self, menu):
        """
        在 系统登录后进入的主页 直接点击进入菜单
        注意：此时未进入任何应用，包括我的地盘
        :param menu: todo, task, project, order, contract, review, company, dynamic
        :return:
        """
        menu_selector = self.get_main_menu_selector(menu)

        self.base_driver.click(menu_selector)
        sleep(2)

    def get_main_menu_selector(self, menu):
        """
        获取指定 menu 的 Selector
        :param menu: todo, task, project, order, contract, review, company, dynamic
        :return:
        """

        if menu == "todo":
            menu_selector = self.MAIN_BAR_MENU_SELECTOR % 1
        elif menu == "task":
            menu_selector = self.MAIN_BAR_MENU_SELECTOR % 2
        elif menu == "project":
            menu_selector = self.MAIN_BAR_MENU_SELECTOR % 3
        elif menu == "order":
            menu_selector = self.MAIN_BAR_MENU_SELECTOR % 4
        elif menu == "contract":
            menu_selector = self.MAIN_BAR_MENU_SELECTOR % 5
        elif menu == "review":
            menu_selector = self.MAIN_BAR_MENU_SELECTOR % 6
        elif menu == "company":
            menu_selector = self.MAIN_BAR_MENU_SELECTOR % 7
        elif menu == "dynamic":
            menu_selector = self.MAIN_BAR_MENU_SELECTOR % 8
        else:
            menu_selector = "error menu"

        return menu_selector

    def get_app_title(self, app):
        """
        根据 app 获取 对应的标题
        :param app:
        :return:
        """
        has_switch_frame = True
        if app == "oa":
            self.base_driver.switch_frame(self.FRAME_APP_OA_SELECTOR)
        elif app == "crm":
            self.base_driver.switch_frame(self.FRAME_APP_CRM_SELECTOR)
        elif app == "cash":
            self.base_driver.switch_frame(self.FRAME_APP_CASH_SELECTOR)
        elif app == "team":
            self.base_driver.switch_frame(self.FRAME_APP_TEAM_SELECTOR)
        elif app == "proj":
            self.base_driver.switch_frame(self.FRAME_APP_PROJECT_SELECTOR)
        elif app == "admin":
            self.base_driver.switch_frame(self.FRAME_APP_ADMIN_SELECTOR)
        elif app == "doc":
            self.base_driver.switch_frame(self.FRAME_APP_DOC_SELECTOR)
        else:
            has_switch_frame = False
        sleep(1)

        if has_switch_frame:
            app_title = self.base_driver.get_text(self.APP_TETLE_SELCTOR)
            self.base_driver.switch_default()
            sleep(1)
        else:
            app_title = "ERROR APP"

        return app_title

    def get_real_name_from_navbar(self):
        """
        获取主页左上角导航栏的 真实姓名
        :return: 真实姓名 str
        """
        return self.base_driver.get_text(self.MAIN_BAR_ACCOUNT_SELECTOR)

    def get_real_name_from_start_menu(self):
        """
        获取主页开始菜单中的 真实姓名
        :return: 真实姓名 str
        """
        self.base_driver.click(self.LOGOUT_AVATAR_SELECTOR)
        sleep(1)
        real_name = self.base_driver.get_text(self.LOGOUT_MENU_ACCOUNT_SELECTOR)
        self.base_driver.click(self.LOGOUT_AVATAR_SELECTOR)
        sleep(1)
        return real_name

    def get_language_from_start_menu(self):
        """
        获取主页开始菜单中的 当前语言
        :return: 当前语言 str
        """
        self.base_driver.click(self.LOGOUT_AVATAR_SELECTOR)
        sleep(1)
        lang = self.base_driver.get_text(self.LOGOUT_MENU_LANG_SELECTOR)
        self.base_driver.click(self.LOGOUT_AVATAR_SELECTOR)
        sleep(1)
        return lang

    def click_menu(self, menu):
        """
        点击主页导航的 菜单
        :param menu:
            待办：todo_calendar
            任务：task
            项目：project
            订单：order
            合同：contract
            审批：review
            组织：company
            动态：dynamic
        :return:
        """
        menu_selector = self._get_menu_selector(menu)

        if menu_selector is not None:
            self.driver.click(menu_selector)
            self.driver.forced_wait(5)

    def do_logout(self, is_admin=False):
        """
        退出登录
        :return:
        """
        self.driver.click(self.START_BUTTON_SELECTOR)
        self.driver.forced_wait(1)

        if is_admin:
            self.driver.click(self.START_EXIT_MENU_SELECTOR % 10)
        else:
            self.driver.click(self.START_EXIT_MENU_SELECTOR % 8)

    def _get_menu_selector(self, menu):
        """
        根据菜单的名字获取菜单的定位符
        :param menu:
            待办：todo_calendar
            任务：task
            项目：project
            订单：order
            合同：contract
            审批：review
            组织：company
            动态：dynamic
        :return:
        """
        menu_selector = None

        if menu == "todo_calendar":
            menu_selector = self.NAV_MENU_SELECTOR % 1

        elif menu == "task":
            menu_selector = self.NAV_MENU_SELECTOR % 2

        elif menu == "project":
            menu_selector = self.NAV_MENU_SELECTOR % 3

        elif menu == "order":
            menu_selector = self.NAV_MENU_SELECTOR % 4

        elif menu == "contract":
            menu_selector = self.NAV_MENU_SELECTOR % 5

        elif menu == "review":
            menu_selector = self.NAV_MENU_SELECTOR % 6

        elif menu == "company":
            menu_selector = self.NAV_MENU_SELECTOR % 7

        elif menu == "dynamic":
            menu_selector = self.NAV_MENU_SELECTOR % 8

        return menu_selector
